<template>
    <div>
        <div class="address-content" :data="addressData">
            <div class="content">
                <div class="address" v-for="(address,index) in addressData" :key="index">
                    <div class="name" @click="chooseTip">{{address.name}}</div>
                    <div class="describe" v-for="(pointAddress,idx) in address.points" :key="idx"
                     @click="updateAddress(address.name,address.id,pointAddress.point_name,pointAddress.point_address)">
                        {{pointAddress.point_name}} {{pointAddress.point_address}}
                    </div>
                </div>
            </div>
        </div>
        <modal class="modal" v-if="showModal" @close="showModal = false">
            <span slot="header"></span>
            <span slot="body">请选择具体配送点</span>
            <div slot="footer">
                <button class="modal-default-button" @click="showModal = false">
                    OK
                </button>
            </div>
        </modal>
    </div>
</template>

<script type="text/ecamscript-6">
import { getAddressList } from "@/api/address";
import modal from "@/base/modal/modal";
import Bus from "@/commom/js/bus";

export default {
  data() {
    return {
      addressData: [],
      showModal: false
    };
  },
  components: {
    modal
  },
  created() {
    // 使用vue推荐的axios方式获取后台地址信息数据
    getAddressList().then(res => {
      this.addressData = res.data;
    });

    if(sessionStorage.getItem("setUserName") == null){
         this.$router.push({
           path: "/login"
        });
    }
  },
  methods: {
    updateAddress(name, id,point_name,point_address) {
      // 使用H5的localStorage特性存储选择的配送信息
      localStorage.setItem("setname", name);  //社区name
      localStorage.setItem("setAddressId", id);  //社区id
      localStorage.setItem("setPointName",point_name);  // 社区配送点name
      localStorage.setItem("setPointAddress",point_address);  // 社区配送点具体地址

      this.$router.push({
        path: `/orders`,
      });
      
    },
    chooseTip() {
      this.showModal = true;
    }
  }
};
</script>

<style scoped lang="stylus" rel="stylesheet/stylus">
.address-content {
    background: #f0f0f0;
    overflow: hidden;
    z-index: 10;

    .now-address {
        padding: 15px 15px 0;
        background-color: #fff;

        .name-content {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 10px;

            .name {
                display: flex;
                align-items: center;

                .iconfont {
                    color: #f20c00;
                    font-size: 20px;
                    margin-right: 5px;
                }

                .name {
                    font-size: 15px;
                    color: #333333;
                    font-weight: bold;
                    line-height: 26px;
                }
            }

            .text {
                color: #f20c00;
                font-size: 12px;
            }
        }

        .describe {
            font-size: 13px;
            color: #666666;
            line-height: 18px;
            padding-bottom: 12px;
            padding-left: 25px;
            border-bottom: 1px solid #eee;
        }
    }

    .content {
        padding: 0 15px;
        background-color: #ffffff;

        .name {
            color: #333;
            font-size: 15px;
            font-weight: bold;
            margin-bottom: 6px;
            padding-top: 18px;
        }

        .describe {
            font-size: 13px;
            color: #666666;
            line-height: 18px;
            padding: 14px 0;
            border-bottom: 1px solid #eee;
        }
    }
}

.loading-container {
    position: absolute;
    width: 100%;
    top: 50%;
    transform: translateY(-50%);
    z-index: 100;
}

.modal {
    .modal-default-button {
        float: right;
        color: #fff;
        background-color: #4aae9b;
        border: 1px solid #4aae9b;
        border-radius: 2px;
        padding: 6px 12px;
        background-color: #4dae9b;
        extent-click();
    }
}
</style>